﻿package com.sxhalo.manage.web.action;

import java.util.*;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.sxhalo.manage.map.SysRoles;
import com.sxhalo.manage.map.SysUsers;
import com.sxhalo.manage.service.IBaseService;
import com.sxhalo.manage.utils.Pager;
import com.sxhalo.manage.web.form.RolesForm;

public class RolesAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private int draw = 1;

	private int start = 0;

	private int length = 20;

	private int userId;

	private RolesForm roles;

	// 将会被Struts2序列化为JSON字符串的对象
	private Map<String, Object> dataMap;

	private List<RolesForm> dataList;

	// 业务层
	private IBaseService<SysRoles> myService;

	private IBaseService<SysUsers> userService;

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public RolesForm getRoles() {
		return roles;
	}

	public void setRoles(RolesForm roles) {
		this.roles = roles;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public List<RolesForm> getDataList() {
		return dataList;
	}

	public void setDataList(List<RolesForm> dataList) {
		this.dataList = dataList;
	}

	public IBaseService<SysRoles> getMyService() {
		return myService;
	}

	public void setMyService(IBaseService<SysRoles> myService) {
		this.myService = myService;
	}

	public IBaseService<SysUsers> getUserService() {
		return userService;
	}

	public void setUserService(IBaseService<SysUsers> userService) {
		this.userService = userService;
	}

	/**
	 * 构造方法
	 */
	public RolesAction() {
		// 初始化Map对象
		dataMap = new HashMap<String, Object>();
	}

	public String getEntity() {
		try {
			SysRoles entity = myService.findById(roles.getRoleId());
			BeanUtils.copyProperties(entity, roles);
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String getGridData() {
		// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
		dataMap.clear();
		// 获取数据
		try {
			int page = start == 0 ? 1 : start / length + 1;
			int size = length < 0 ? Integer.MAX_VALUE : length;
			Pager<SysRoles> list = myService.findPagerList(page, size, "", "");
			dataMap.put("draw", draw);
			dataMap.put("recordsTotal", list.getTotal());
			dataMap.put("recordsFiltered", list.getTotal());
			dataMap.put("data", list.getList());
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String getAllData() {
		// 获取数据
		try {
			List<RolesForm> data = new ArrayList<RolesForm>();
			List<SysRoles> list = myService.findAll();
			for (SysRoles po : list) {
				RolesForm vo = new RolesForm();
				BeanUtils.copyProperties(po, vo);
				data.add(vo);
			}
			dataList = data;
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String addEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysRoles entity = new SysRoles();
		BeanUtils.copyProperties(roles, entity);
		try {
			myService.save(entity);
			dataMap.put("data", true);
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String editEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysRoles entity = myService.findById(roles.getRoleId());
		entity.setRoleName(roles.getRoleName());
		entity.setRoleDescription(roles.getRoleDescription());
		entity.setRoleIsdisable(roles.getRoleIsdisable());
		try {
			myService.update(entity);
			dataMap.put("data", true);
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String delEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		try {
			  SysRoles entity = myService.findById(roles.getRoleId());
			  if(myService.delete(entity)){
				  dataMap.put("valid", true);
				} else {
					dataMap.put("valid", false);
				}
			} catch (Exception re) {
				dataMap.put("valid", false);
			}
		// 返回结果
		return SUCCESS;
	}

	public String getUserRoleList() {
		// 清空dataMap中的数据
		dataMap.clear();
		try {
			SysUsers entity = userService.findById(userId);
			List<SysRoles> allRoles = myService.findAll();
			if (entity != null) {
				Set<SysRoles> roles = entity.getSysRoleses();
				for (SysRoles ro : roles) {
					allRoles.remove(ro);
				}
				dataMap.put("left", allRoles);
				dataMap.put("right", roles.toArray());
			}
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String addUserRoles() {
		// 清空dataMap中的数据
		dataMap.clear();
		try {
			SysUsers user = userService.findById(userId);
			if (user != null) {
				Set<SysRoles> data = new HashSet<SysRoles>(0);
				if (dataList != null) {
					for (RolesForm role : dataList) {
						SysRoles entity = myService.findById(role.getRoleId());
						data.add(entity);
					}
				}
				user.setSysRoleses(data);
				userService.save(user);
			}
			dataMap.put("data", true);
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}
}
